package top.wyloop.intoeins.module.iam.initializer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Component;
import top.wyloop.intoeins.module.iam.model.consts.InitPermissionConst;
import top.wyloop.intoeins.module.iam.model.consts.InitRoleConst;
import top.wyloop.intoeins.module.iam.model.entity.RolePermission;
import top.wyloop.intoeins.module.iam.repository.RolePermissionRepository;

/**
 * 角色授权初始化
 *
 * @author Wyatt6
 * @date 2025/2/1
 */
@Slf4j
@Component
@Order(4)
public class GrantsInitializer implements CommandLineRunner {
    @Autowired
    private RolePermissionRepository repository;

    @Override
    public void run(String... args) throws Exception {
        grantToRoleSuperAdmin();
        grantToRoleAdmin();
    }

    private void grantToRoleSuperAdmin() {
        log.info("[[为超级管理员角色授权]]");
        log.info("--> 授权permission资源权限");
        try {
            repository.save(new RolePermission(InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.PERMISSION_ADD_ID));
            log.info("已完成: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.PERMISSION_ADD_ID);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.PERMISSION_ADD_ID);
        }
        try {
            repository.save(new RolePermission(InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.PERMISSION_DELETE_ID));
            log.info("已完成: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.PERMISSION_DELETE_ID);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.PERMISSION_DELETE_ID);
        }
        try {
            repository.save(new RolePermission(InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.PERMISSION_EDIT_ID));
            log.info("已完成: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.PERMISSION_EDIT_ID);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.PERMISSION_EDIT_ID);
        }
        try {
            repository.save(new RolePermission(InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.PERMISSION_QUERY_ID));
            log.info("已完成: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.PERMISSION_QUERY_ID);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.PERMISSION_QUERY_ID);
        }
        log.info("--> 授权role资源权限");
        try {
            repository.save(new RolePermission(InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.ROLE_ADD_ID));
            log.info("已完成: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.ROLE_ADD_ID);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.ROLE_ADD_ID);
        }
        try {
            repository.save(new RolePermission(InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.ROLE_DELETE_ID));
            log.info("已完成: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.ROLE_DELETE_ID);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.ROLE_DELETE_ID);
        }
        try {
            repository.save(new RolePermission(InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.ROLE_EDIT_ID));
            log.info("已完成: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.ROLE_EDIT_ID);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.ROLE_EDIT_ID);
        }
        try {
            repository.save(new RolePermission(InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.ROLE_QUERY_ID));
            log.info("已完成: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.ROLE_QUERY_ID);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.ROLE_QUERY_ID);
        }
        log.info("--> 授权user资源权限");
        try {
            repository.save(new RolePermission(InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.USER_ADD_ID));
            log.info("已完成: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.USER_ADD_ID);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.USER_ADD_ID);
        }
        try {
            repository.save(new RolePermission(InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.USER_DELETE_ID));
            log.info("已完成: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.USER_DELETE_ID);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.USER_DELETE_ID);
        }
        try {
            repository.save(new RolePermission(InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.USER_EDIT_ID));
            log.info("已完成: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.USER_EDIT_ID);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.USER_EDIT_ID);
        }
        try {
            repository.save(new RolePermission(InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.USER_QUERY_ID));
            log.info("已完成: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.USER_QUERY_ID);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.USER_QUERY_ID);
        }
        log.info("--> 授权身份认证管理菜单权限");
        try {
            repository.save(new RolePermission(InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.MENU_IAM_ID));
            log.info("已完成: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.MENU_IAM_ID);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.MENU_IAM_ID);
        }
        try {
            repository.save(new RolePermission(InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.MENU_USER_MANAGE_ID));
            log.info("已完成: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.MENU_USER_MANAGE_ID);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.MENU_USER_MANAGE_ID);
        }
        try {
            repository.save(new RolePermission(InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.MENU_ROLE_MANAGE_ID));
            log.info("已完成: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.MENU_ROLE_MANAGE_ID);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.MENU_ROLE_MANAGE_ID);
        }
        try {
            repository.save(new RolePermission(InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.MENU_PERMISSION_MANAGE_ID));
            log.info("已完成: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.MENU_PERMISSION_MANAGE_ID);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.MENU_PERMISSION_MANAGE_ID);
        }
        log.info("--> 授权API权限");
        try {
            repository.save(new RolePermission(InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.UPDATE_GRANTS_ID));
            log.info("已完成: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.UPDATE_GRANTS_ID);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.UPDATE_GRANTS_ID);
        }
        try {
            repository.save(new RolePermission(InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.UPDATE_BINDS_ID));
            log.info("已完成: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.UPDATE_BINDS_ID);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: (ROLE, PERM) = ({}, {})", InitRoleConst.SUPER_ADMIN_ROLE_ID, InitPermissionConst.UPDATE_BINDS_ID);
        }
    }

    private void grantToRoleAdmin() {
        log.info("[[为管理员角色授权]]");
        log.info("--> 授权permission资源权限");
        try {
            repository.save(new RolePermission(InitRoleConst.ADMIN_ROLE_ID, InitPermissionConst.PERMISSION_QUERY_ID));
            log.info("已完成: (ROLE, PERM) = ({}, {})", InitRoleConst.ADMIN_ROLE_ID, InitPermissionConst.PERMISSION_QUERY_ID);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: (ROLE, PERM) = ({}, {})", InitRoleConst.ADMIN_ROLE_ID, InitPermissionConst.PERMISSION_QUERY_ID);
        }
        log.info("--> 授权role资源权限");
        try {
            repository.save(new RolePermission(InitRoleConst.ADMIN_ROLE_ID, InitPermissionConst.ROLE_QUERY_ID));
            log.info("已完成: (ROLE, PERM) = ({}, {})", InitRoleConst.ADMIN_ROLE_ID, InitPermissionConst.ROLE_QUERY_ID);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: (ROLE, PERM) = ({}, {})", InitRoleConst.ADMIN_ROLE_ID, InitPermissionConst.ROLE_QUERY_ID);
        }
        log.info("--> 授权user资源权限");
        try {
            repository.save(new RolePermission(InitRoleConst.ADMIN_ROLE_ID, InitPermissionConst.USER_ADD_ID));
            log.info("已完成: (ROLE, PERM) = ({}, {})", InitRoleConst.ADMIN_ROLE_ID, InitPermissionConst.USER_ADD_ID);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: (ROLE, PERM) = ({}, {})", InitRoleConst.ADMIN_ROLE_ID, InitPermissionConst.USER_ADD_ID);
        }
        try {
            repository.save(new RolePermission(InitRoleConst.ADMIN_ROLE_ID, InitPermissionConst.USER_DELETE_ID));
            log.info("已完成: (ROLE, PERM) = ({}, {})", InitRoleConst.ADMIN_ROLE_ID, InitPermissionConst.USER_DELETE_ID);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: (ROLE, PERM) = ({}, {})", InitRoleConst.ADMIN_ROLE_ID, InitPermissionConst.USER_DELETE_ID);
        }
        try {
            repository.save(new RolePermission(InitRoleConst.ADMIN_ROLE_ID, InitPermissionConst.USER_EDIT_ID));
            log.info("已完成: (ROLE, PERM) = ({}, {})", InitRoleConst.ADMIN_ROLE_ID, InitPermissionConst.USER_EDIT_ID);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: (ROLE, PERM) = ({}, {})", InitRoleConst.ADMIN_ROLE_ID, InitPermissionConst.USER_EDIT_ID);
        }
        try {
            repository.save(new RolePermission(InitRoleConst.ADMIN_ROLE_ID, InitPermissionConst.USER_QUERY_ID));
            log.info("已完成: (ROLE, PERM) = ({}, {})", InitRoleConst.ADMIN_ROLE_ID, InitPermissionConst.USER_QUERY_ID);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: (ROLE, PERM) = ({}, {})", InitRoleConst.ADMIN_ROLE_ID, InitPermissionConst.USER_QUERY_ID);
        }
        log.info("--> 授权菜单权限");
        try {
            repository.save(new RolePermission(InitRoleConst.ADMIN_ROLE_ID, InitPermissionConst.MENU_IAM_ID));
            log.info("已完成: (ROLE, PERM) = ({}, {})", InitRoleConst.ADMIN_ROLE_ID, InitPermissionConst.MENU_IAM_ID);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: (ROLE, PERM) = ({}, {})", InitRoleConst.ADMIN_ROLE_ID, InitPermissionConst.MENU_IAM_ID);
        }
        try {
            repository.save(new RolePermission(InitRoleConst.ADMIN_ROLE_ID, InitPermissionConst.MENU_USER_MANAGE_ID));
            log.info("已完成: (ROLE, PERM) = ({}, {})", InitRoleConst.ADMIN_ROLE_ID, InitPermissionConst.MENU_USER_MANAGE_ID);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: (ROLE, PERM) = ({}, {})", InitRoleConst.ADMIN_ROLE_ID, InitPermissionConst.MENU_USER_MANAGE_ID);
        }
        try {
            repository.save(new RolePermission(InitRoleConst.ADMIN_ROLE_ID, InitPermissionConst.MENU_ROLE_MANAGE_ID));
            log.info("已完成: (ROLE, PERM) = ({}, {})", InitRoleConst.ADMIN_ROLE_ID, InitPermissionConst.MENU_ROLE_MANAGE_ID);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: (ROLE, PERM) = ({}, {})", InitRoleConst.ADMIN_ROLE_ID, InitPermissionConst.MENU_ROLE_MANAGE_ID);
        }
        try {
            repository.save(new RolePermission(InitRoleConst.ADMIN_ROLE_ID, InitPermissionConst.MENU_PERMISSION_MANAGE_ID));
            log.info("已完成: (ROLE, PERM) = ({}, {})", InitRoleConst.ADMIN_ROLE_ID, InitPermissionConst.MENU_PERMISSION_MANAGE_ID);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: (ROLE, PERM) = ({}, {})", InitRoleConst.ADMIN_ROLE_ID, InitPermissionConst.MENU_PERMISSION_MANAGE_ID);
        }
        log.info("--> 授权API权限");
        try {
            repository.save(new RolePermission(InitRoleConst.ADMIN_ROLE_ID, InitPermissionConst.UPDATE_BINDS_ID));
            log.info("已完成: (ROLE, PERM) = ({}, {})", InitRoleConst.ADMIN_ROLE_ID, InitPermissionConst.UPDATE_BINDS_ID);
        } catch (DataIntegrityViolationException e) {
            log.info("已存在: (ROLE, PERM) = ({}, {})", InitRoleConst.ADMIN_ROLE_ID, InitPermissionConst.UPDATE_BINDS_ID);
        }
    }
}
